﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections.Generic;
using System.Linq;
using Pipra.Math.Geometry;

namespace Pipra.Gis.Projection
{
    /// <summary>
    /// A lambert conic conformal projection with 1 standard parallel.
    /// </summary>
    public class LambertConicConformal1Sp : LambertConicConformal
    {

        internal new const string DefaultName = LambertConicConformal.DefaultName + " 1SP";

        /// <summary>
        /// Origin scale factor.
        /// </summary>
        public readonly double OriginScaleFactor;

        /// <summary>
        /// Constructs a new lambert conic conformal projection from 1 standard parallel.
        /// </summary>
        /// <param name="geographiOrigin">The geographic origin.</param>
        /// <param name="originScaleFactor">The scale factor at the origin.</param>
        /// <param name="falseProjectedOffset">The false projected offset.</param>
        /// <param name="spheroid">The spheroid.</param>
        public LambertConicConformal1Sp(
            LatLon geographiOrigin,
            double originScaleFactor,
            ICoordinatePair<double> falseProjectedOffset,
            ISpheroid<double> spheroid
        )
            : base(geographiOrigin, falseProjectedOffset, spheroid)
        {
            OriginScaleFactor = originScaleFactor;
            double originParallelSin = System.Math.Sin(geographiOrigin.Lat);
            double eParallelSin = E * originParallelSin;
            double tOrigin = (
                System.Math.Tan(QuarterPi - (geographiOrigin.Lat / 2.0))
                /
                System.Math.Pow(
                    (1.0 - eParallelSin) / (1.0 + eParallelSin),
                    EHalf
                )
            );
            N = System.Math.Sin(geographiOrigin.Lat);
            if(0 == N)
            {
                throw new ArgumentException("Invalid N value.");
            }
            F = (
                (
                    System.Math.Cos(geographiOrigin.Lat)
                    /
                    System.Math.Sqrt(1.0 - (ESq * originParallelSin * originParallelSin))
                )
                /
                (
                    N * System.Math.Pow(tOrigin, N)
                )
            );
            Af = MajorAxis * F * originScaleFactor;
            ROrigin = Af * System.Math.Pow(tOrigin, N);
            Invn = 1.0 / N;
            NorthingOffset = falseProjectedOffset.Y + ROrigin;
        }

        public override string Name
        {
            get { return DefaultName; }
        }

        public override IEnumerable<INamedParameter> GetParameters()
        {
            return base.GetParameters()
                .Concat(new INamedParameter[]
                    {
                        new NamedParameter<double>("scale factor", OriginScaleFactor)
                    }
                )
            ;
        }

    }
}
